package unit6;

class SimpleThread extends Thread {
    private int count = 2; //循环次数
    public SimpleThread() {//调用父类无带参构造
    }
    public SimpleThread(String name) {
        super(name); //指定调用父类带参构造
    }
    public void run() {
        for (int i = 0; i < count; i++) {
            //getName()获得当前线程名
            System.out.println(getName() + ":" + i);
        }
    }
}
class MyRunnableThread implements Runnable {
    private int count = 2; //循环次数
    @Override
    public void run() {
        for (int i = 0; i < count; i++) {
            //用静态方法currentThread()获得当前线程
            System.out.println(Thread.currentThread().getName() + ":" + i);
        }
    }
}

public class Ex6_1_ThreadTest {
    public static void main(String[] args) {
        ////创建自定义线程类simpleThread1对象，不指定线程名
        SimpleThread simpleThread1 = new SimpleThread();
        //使用带名字String参的构造方法创建线程对象
        SimpleThread simpleThread2 = new SimpleThread("新线程2");
        //创建自定义线程类myRunnableThread对象，不指定线程名
        MyRunnableThread myRunnableThread = new MyRunnableThread();
        //使用带myRunnableThread参数的构造方法创建线程对象
        Thread thread3 = new Thread(myRunnableThread);
        //使用带两个参的构造方法创建线程对象
        Thread thread4 = new Thread(myRunnableThread, "新线程4");
        simpleThread1.start();
        simpleThread2.start();
        thread3.start();
        thread4.start();
    }
}
